<?php

namespace App\Http\Controllers\Business;

use App\Models\Manager;
use App\Models\VerifyCode;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;

class BaseController extends Controller
{

    public $messageLimit = 10;
    /**
     * @return array
     * 短信验证码
     */
    public function getCode(Request $request)
    {
        if(preg_match('/^[1][3,4,5,6,7,8,9][0-9]{9}$/', $request->mobile)) {
            if($request->type == 'register')
            {
                $str = '您正在申请成为渠道经理，';
            }elseif($request->type == 'forgot'){
                $str = '您正在申请找回密码操作，';
            }else{
                $str = '';
            }
            $perfix = Session::get('manager.perfix');
            //手机端：注册无openid
            //微信：全部存在
            $exist = Manager::where(['phone' => $request->mobile])->first();
            if($request->type == 'register' && $exist)
            {//判断是否注册过：
//                if($perfix=='wap') {
                    $this->set('code', 1);
                    $this->set('message', '已经注册过');
               /* }else{
                }*/
            }elseif($request->type == 'forgot' && empty($exist))
            {//判断是否注册过：
                $this->set('code',1);
                $this->set('message','未注册过');
            }else {
                //判断当日该手机号的短信条数：
                $counts = VerifyCode::where(['phone'=>$request->mobile])
                    ->whereDate('created_at', date('Y-m-d'))
                    ->count();
                if($counts < $this->messageLimit) {
                    $code = rand(100000, 999999);
                    $send = $this->sendMsg($request->mobile, $code, $str);
                    if ($send['code'] > 0) {
                        $insert = VerifyCode::create(['phone' => $request->mobile, 'verify_code' => $code]);
                        if ($insert) {
                            Session::put('manager.verify.' . $request->mobile, $code);
                            Session::save();
                            $this->set('data', ['code' => $code]);
                        } else {
                            $this->set('msg', 'error');
                        }
                    } else {
                        $this->set('code', 1);
                    }
                }else{
                    $this->set('code', 1);
                    $this->set('message','验证码申请次数达上限');
                }
            }
        }else{
            $this->set('code',1);
            $this->set('msg','手机号码不正确');
        }
        return $this->ajaxResponse();
    }

    /**
     * @param Request $request
     * @return array
     * 验证码 验证
     */
    public function checkCode(Request $request){
        $validate = Validator::make($request->all(),[
            'mobile' => 'required|min:11|max:11',
            'code' => 'required'
        ]);
        if(!$validate->fails()) {
            if(Session::get('manager.verify.' . $request->mobile) == $request->code)
            {
                Session::forget('manager.verify.' . $request->mobile);
            }else{
                $this->set('code',1);
                $this->set('message','验证失败');
            }
        }else{
            $this->set('code',1);
            $this->set('message','验证失败');
        }
        return $this->ajaxResponse();
    }

    /**
     * 检查邀请码
     */
    function checkInviteCode(Request $request){
        if($request->code){
            $exist = Manager::where(['invitation_code'=>$request->code])->first();
            if(!$exist){
                $this->set('code',1);
                $this->set('message','请输入正确的邀请码或留空');
            }
        }else{
            $this->set('code',1);
            $this->set('message','邀请码为空');
        }
        return $this->ajaxResponse();
    }
}
